package com.douding.server.service;

import com.douding.server.domain.*;
import com.douding.server.dto.*;
import com.douding.server.enums.CourseStatusEnum;
import com.douding.server.mapper.CategoryMapper;
import com.douding.server.mapper.CourseContentMapper;
import com.douding.server.mapper.CourseMapper;
import com.douding.server.mapper.my.MyCourseMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


import java.util.Date;


@Service
public class CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private MyCourseMapper myCourseMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CourseCategoryService myCategoryService;

    @Resource
    private CourseContentMapper courseContentMapper;

    @Resource
    private TeacherService teacherService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private SectionService sectionService;


    private static final Logger LOG = LoggerFactory.getLogger(CourseService.class);


    /**
     * 列表查询：关联课程分类表 web接口
     *
     * @param pageDto
     */
    public void list(CoursePageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        CourseExample courseExample = new CourseExample();
        List<Course> courseList = courseMapper.selectByExample(courseExample);
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());
        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);
        // 查找课程分类categories
        for (CourseDto each : courseDtoList) {
            // 根据课程id获取分类列表
            List<CourseCategoryDto> courseCategoryDtos = myCategoryService.listByCourse(each.getId());
            List<CategoryDto> categoryDtoList = new ArrayList<>();
            for (CourseCategoryDto courseCategoryDto: courseCategoryDtos) {
                // 获取分类
                Category category = categoryMapper.selectByPrimaryKey(courseCategoryDto.getCategoryId());
                CategoryDto categoryDto = CopyUtil.copy(category, CategoryDto.class);
                categoryDtoList.add(categoryDto);
            }
            each.setCategorys(categoryDtoList);
        }
        // 查找讲师列表，对应课程讲师
        List<TeacherDto> teacherDtoList = teacherService.all();
        for (TeacherDto eachTeacher : teacherDtoList) {
            String teacherId = eachTeacher.getId();
            for (CourseDto item : courseDtoList) {
                if (item.getTeacherId().equals(teacherId)) {
                    item.setTeacher(eachTeacher);
                }
            }
        }
        // TODO chapter section
        pageDto.setList(courseDtoList);
    }


    public void save(CourseDto courseDto) {
        Course course = CopyUtil.copy(courseDto, Course.class);
        if (StringUtils.isEmpty(course.getId())) {
            this.insert(course);
        } else {
            this.update(course);
        }
    }

    //新增数据
    private void insert(Course course) {
        course.setId(UuidUtil.getShortUuid());
        courseMapper.insert(course);

    }

    //更新数据
    private void update(Course course) {
        courseMapper.updateByPrimaryKey(course);
    }

    public void delete(String id) {
        courseMapper.deleteByPrimaryKey(id);
    }

    //更新课程时长
    public void updateTime(@Param("courseId") String courseId) {

    }

    //课程内容相关的操作 查找 新增,修改
    public CourseContentDto findContent(String id) {
        CourseContent courseContent = courseContentMapper.selectByPrimaryKey(id);
        CourseContentDto courseContentDto = CopyUtil.copy(courseContent, CourseContentDto.class);
        return courseContentDto;
    }

    //新增内容 或者修改内容
    public int saveContent(CourseContentDto contentDto) {
        CourseContent content = CopyUtil.copy(contentDto, CourseContent.class);
        if (StringUtils.isEmpty(content.getId())) {
            courseContentMapper.insert(content);
        } else {
            courseContentMapper.updateByPrimaryKeySelective(content);
        }
        return 0;
    }


    public void sort(SortDto sortDto) {

    }

    /**
     * 查找某一课程，供web模块用，只能查已发布的
     *
     * @param id
     * @return
     */
    public CourseDto findCourse(String id) {
        CourseContent courseContent = courseContentMapper.selectByPrimaryKey(id);
        CourseDto courseDto = CopyUtil.copy(courseContent, CourseDto.class);
        return courseDto;
    }

    /**
     * 新课列表查询，只查询已发布的，按创建日期倒序
     */
    public List<CourseDto> listNew(PageDto pageDto) {

        return null;
    }


}//end class
